package com.study.register.controller;

import com.aliyuncs.exceptions.ClientException;
import com.study.common.entity.LoginException;
import com.study.common.entity.ResultInfo;
import com.study.common.entity.User;
import com.study.common.enumerate.ExceptionInfo;
import com.study.common.pagination.Pagination;
import com.study.common.utils.AliyunMessageUtils;
import com.study.common.utils.I18nUtils;
import com.study.common.utils.ResultUtils;
import com.study.common.utils.SafetyUtils;
import com.study.main.dao.MainDao;
import com.study.main.service.MainService;
import com.study.main.vo.UserInfo;
import com.study.register.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @Author 谭永超 [www.tanyongchao.tk:8090/SumDome/index]
 * @Date 2018/8/31 13:47
 * @Description 注册页面
 */
@Controller
@RequestMapping(value = "/register")
public class RegisterController {
    @Autowired
    @Qualifier(value = "registerService")
    private RegisterService registerService;

    @RequestMapping(value = "/index", method = {RequestMethod.GET})
    public String register() {

        return "register";
    }

    /**
     * 注册用户
     *
     * @param locale  返回的语言类型
     * @param user    提交的用户信息
     * @param session 内置session
     * @return com.study.common.entity.ResultInfo
     */
    @RequestMapping(value = "/subRegister ", method = {RequestMethod.POST})
    @ResponseBody
    public ResultInfo query(Locale locale, @ModelAttribute User user, HttpSession session) {
        //AliyunMessageUtils messageUtils = new AliyunMessageUtils();
        ResultUtils resultUtils = new ResultUtils();
        try {
            User user1 = registerService.findByUserName(user.getUserName());
            User user2 = registerService.findByTelNumber(user.getTelNumber());
            if (user1 == null) {
                if (user2 == null) {
                    registerService.save(user);
                    return resultUtils.success(null, I18nUtils.getrb(locale, "registeredSuccess"));
                } else {
                    return resultUtils.fail(I18nUtils.getrb(locale, "phone"));
                }
            } else {
                return resultUtils.fail(I18nUtils.getrb(locale, "duplicateUsername"));
            }

        } catch (Exception e) {
            return resultUtils.fail(I18nUtils.getrb(locale, "registeredFailed"));
        }
    }

    /**
     * 获取短信验证码
     *
     * @param telNumber 电话号码
     * @param session
     * @param model
     * @return
     */
    @RequestMapping(value = "/sendCode", method = {RequestMethod.POST})
    @ResponseBody
    public ResultInfo<Pagination> sendCode(Locale locale, @RequestParam("phoneNumber") String telNumber, HttpSession session, Model model) {
        ResultUtils<Pagination> resultUtils = new ResultUtils();
        try {
            // 判断电话号码是否存在
            User user = registerService.findByTelNumber(telNumber);
            if (!user.equals("")) {
                String code = registerService.sendCode(telNumber, session);
                if (code == "发送请求过于平凡请耐心等待") {
                    return resultUtils.fail(I18nUtils.getrb(locale, "send"));
                }
                session.setAttribute(code + "a", code);
                session.setAttribute(telNumber + "a", telNumber);
            }
            return resultUtils.success(null, I18nUtils.getrb(locale, "sendSuccess"));
        } catch (Exception e) {
            return resultUtils.fail(I18nUtils.getrb(locale, "sendFailed"));
        }
    }

    /**
     * @param password  新输入的密码
     * @param inputCode
     * @param session
     * @param locale
     * @return com.study.common.entity.ResultInfo
     */
    @RequestMapping(value = "/forgetPWD", method = {RequestMethod.POST})
    @ResponseBody
    public ResultInfo forgetPWD(@RequestParam("phoneNumber") String telNumber, @RequestParam("newPassword_1") String password, @RequestParam("newPassword_2") String password1, @RequestParam("msg") String inputCode, HttpSession session, Locale locale) {
        User user = new User();
        String code = (String) session.getAttribute(inputCode + "a");
        if (code != null) {
            if (code.equals(inputCode)) {
                if (password.equals(password1)) {
                    String phoneNumber = (String) session.getAttribute(telNumber + "a");
                    user.setTelNumber(phoneNumber);
                    user.setPassWord(password);
                    registerService.updateByPassword(user);
                    return new ResultUtils().success(session, I18nUtils.getrb(locale, "updateSuccess"));
                } else {
                    return new ResultUtils().fail(I18nUtils.getrb(locale, "passwordsDifferent"));
                }
            } else {
                return new ResultUtils().fail(I18nUtils.getrb(locale, "verification"));
            }
        } else {
            return new ResultUtils().fail(I18nUtils.getrb(locale, "verification"));
        }
    }
}